import { makeObservable, action } from 'mobx';
import { message } from 'antd';
import Obverse, { defaultResumeDetailModalConfig } from './obverse';

import { getResumeDetail } from '@/request/api/resume';

class Resume extends Obverse {
    constructor() {
        super();
        makeObservable(this);
    }

    // 打开人才详情弹窗
    @action.bound
    public openResumeDetailModal(talentId: string, name: string) {
        this.resumeDetailModalConfig.name = name;
        this.resumeDetailModalConfig.visible = true;
        this.resumeDetailModalConfig.loading = true;

        getResumeDetail(talentId)
            .then((res) => {
                if (res.data.code === 200 && res.data.result) {
                    this.resumeDetailModalConfig.result = res.data.result;
                } else {
                    throw new Error(
                        res.data.result || '获取人才详情失败, 请联系管理员！'
                    );
                }
            })
            .catch((e) => {
                message.error(e.message);
                this.closeResumeDetailModal();
            })
            .finally(() => {
                this.resumeDetailModalConfig.loading = false;
            });
    }

    // 关闭人才详情弹窗
    @action.bound
    public closeResumeDetailModal() {
        this.resumeDetailModalConfig = Object.assign(
            {},
            defaultResumeDetailModalConfig
        );
    }
}

export default new Resume();
